<?php
/**
* @file $Id: _makeLetterGrade.fnc.php 300 2006-11-18 00:31:36Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function _makeLetterGrade($percent,$course_period_id)
{	global $programconfig,$_FOCUS,$grade_scale_id;

	if(!$grade_scale_id)
	{
		$grade_scale_id = DBGet(DBQuery("SELECT cp.COURSE_ID,cp.TITLE,c.TITLE,cw.CREDITS,cw.GPA_MULTIPLIER AS COURSE_TITLE,cp.DOES_GRADES,cp.GRADE_SCALE_ID FROM COURSE_PERIODS cp,COURSES c,COURSE_WEIGHTS cw WHERE cw.COURSE_ID=cp.COURSE_ID AND cw.COURSE_WEIGHT=cp.COURSE_WEIGHT AND c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_PERIOD_ID='".$course_period_id."'"));
		$grade_scale_id = $grade_scale_id[1]['GRADE_SCALE_ID'];
	}
	
	if(!$programconfig)
	{
		$config_RET = DBGet(DBQuery("SELECT TITLE,VALUE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='Gradebook'"),array(),array('TITLE'));
		if(count($config_RET))
		{
			foreach($config_RET as $title=>$value)
				$programconfig[$title] = $value[1]['VALUE'];
		}
	}
	if($programconfig['ROUNDING']=='UP')
		$percent = ceil($percent*100);
	elseif($programconfig['ROUNDING']=='DOWN')
		$percent = floor($percent*100);
	else
		$percent = round($percent*100,0);

	if(!$_FOCUS['_makeLetterGrade']['grades'])
		$_FOCUS['_makeLetterGrade']['grades'] = DBGet(DBQuery("SELECT TITLE,ID,DEFAULT_BREAKOFF FROM REPORT_CARD_GRADES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND SCALE_ID='".$grade_scale_id."' ORDER BY SORT_ORDER"));
	//$grades = array('A+','A','A-','B+','B','B-','C+','C','C-','D+','D','D-','F');

	$custom = false;
	foreach($_FOCUS['_makeLetterGrade']['grades'] as $grade)
	{
		if($programconfig[$course_period_id.'-'.$grade['ID']])
		{
			$custom = true;
			break;
		}
	}

	if(!$custom)
	{
		foreach($_FOCUS['_makeLetterGrade']['grades'] as $grade)
			$programconfig[$course_period_id.'-'.$grade['ID']] = $grade['DEFAULT_BREAKOFF'];
	}
	
	foreach($_FOCUS['_makeLetterGrade']['grades'] as $grade)
	{
		if($percent >= $programconfig[$course_period_id.'-'.$grade['ID']] && is_numeric($programconfig[$course_period_id.'-'.$grade['ID']]))
			return $grade['TITLE'];
	}
}
?>